package cn.alpha.intell.rbtlib.wifi.dlalog;

import android.app.Dialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.rbt.utils.ButtonSound;


/**
 * Created by huangzhiqin on 2016/9/28.
 */
public class WIFIDetalsDialog extends Dialog {

    public WIFIDetalsDialog(Context context) {
        super(context);
    }

    public WIFIDetalsDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void applyCompat() {
        if (Build.VERSION.SDK_INT < 19) {
            return;
        }
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        applyCompat();
        super.onCreate(savedInstanceState);
    }


    public static class Builder {
        private static final String TAG = "WiFiDetailsDialog";
        private Context context;
        private WifiInfo mWifiInfo;
        private WifiDetailsCallBack mCallBack;
        public Builder(Context context) {
            this.context = context;
        }
        public Builder setNetWorkInfo(WifiInfo workInfo){
            this.mWifiInfo = workInfo;
            return  this;
        }
        public Builder setCallBack(WifiDetailsCallBack callBack){
            this.mCallBack = callBack;
            return  this;
        }

        public WIFIDetalsDialog create() {

            final WIFIDetalsDialog dialog = new WIFIDetalsDialog(context, R.style.Dialog);

            LayoutInflater inflater = dialog.getLayoutInflater();

            View view = inflater.inflate(R.layout.dialog_wifi_details,null);
            ImageButton imageButtonFinished =(ImageButton)view.findViewById(R.id.wifi_details_fininsh_btn);
            ImageButton imageButtonDelete = (ImageButton)view.findViewById(R.id.wifi_details_delete_btn);
            TextView tvConnStstus,tvConnectLevel,tvConnSpeed,tvFrequent;
            tvConnStstus = (TextView) view.findViewById(R.id.wifi_detail_status_info);
            tvConnectLevel = (TextView) view.findViewById(R.id.wifi_detail_signal_strength);
            tvConnSpeed = (TextView) view.findViewById(R.id.wifi_detail_connection_speed);
            tvFrequent = (TextView) view.findViewById(R.id.wifi_detail_frequency);
            TextView wifiDetailTitle=(TextView)view.findViewById(R.id.dialog_wifi_detail_title);
            ImageView imageViewClose=(ImageView)view.findViewById(R.id.dialog_wifi_detail_close);
            imageButtonFinished.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ButtonSound.playOgg(context);
                    dialog.dismiss();
                }
            });

            imageButtonDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ButtonSound.playOgg(context);
                    if(mWifiInfo!=null){
                      int networkid =  mWifiInfo.getNetworkId();
                        if(mCallBack!=null){
                            mCallBack.onDelete(networkid);
                        }
                        dialog.dismiss();
                    }
                }
            });

            imageViewClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            if(mWifiInfo!=null) {
                wifiDetailTitle.setText(mWifiInfo.getSSID().replace("\"",""));
                tvConnStstus.setText("已连接");
                tvConnectLevel.setText(getLevel(WifiManager.calculateSignalLevel(mWifiInfo.getRssi(),6)));
                tvConnSpeed.setText(mWifiInfo.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS);
                tvFrequent.setText(mWifiInfo.getFrequency() + WifiInfo.FREQUENCY_UNITS);
            }

            dialog.setContentView(view);

            WindowManager.LayoutParams windowlp =  dialog.getWindow().getAttributes();
            windowlp.width = 800;
            windowlp.height = 600;
            windowlp.gravity = Gravity.CENTER;
            dialog.getWindow().setAttributes(windowlp);

            return dialog;
        }


        private String getLevel(int level){
            if(level<=1){
                return  "弱";
            }else if (level<=3){
                return  "中";
            }else {
                return "强";
            }

        }

        public interface WifiDetailsCallBack{
          void   onDelete(int networkID);
          void   onFinished();

        }
    }
}
